<?php
defined('DCS_ROOT') or exit(header("HTTP/1.1 430 Not Forbidden"));

class countchart {

	function __construct (){
		$this->url = getUrl();
		$action = $this->url['action'];			
		$params = $this->url['params']['1'];
		if($params == 'comment'){
			$this->commentCounts();
		}elseif($params == 'rating'){
			$this->ratingCounts();
		}else{
			$this->defaultAction();
		}
		exit();
	}

	function defaultAction(){
		exit('ssd');
	}


	function commentCounts(){
		require_once(APP_PATH."model/rating.php" );
		$db  = getDB();
		$data = new RatingModel($db);
		$id = $this->url['params']['2'];
		$movie = $data->getMovie($id);
		empty($movie) && exit();

		$comments = $data->getTwoWeekComments($id);
		$reviews = $data->getTwoWeekReviews($id);

		$cList  = getArchive($comments);
		$rList  = getArchive($reviews);
		if($cList){
			$cMax = max($cList);
		}
		if($rList){
			$rMax = max($rList);
		}

		$max = $cMax['count'] >= $rMax['count'] ?  $cMax['count'] : $rMax['count'];

		$s = ceil($max / 10);

		$dateList = getDateList();

		foreach($dateList as $k => $v ){
			if(!empty($cList[$v])){
				$cNum .= $cList[$v]['count'].',';
			}else{
				$cNum .= '0,';
			}
			if(!empty($rList[$v])){
				$rNum .= $rList[$v]['count'].',';
			}else{
				$rNum .= '0,';
			}
		}
		
		foreach($dateList as $k => $v ){
			$date .= '"'.$v.'",';
		}
		
		$cNum = substr($cNum,0,-1);
		$rNum = substr($rNum,0,-1);
		$date = substr($date,0,-1);

		if($max <= '0'){
			$max = '10';
		}
		
		echo '
{
	"elements":[
		{
			"type":"line",
			"dot-style": {"type":"solid-dot", "dot-size": 2, "halo": 1},
			"values":['.$cNum.'],
			"colour":"#FFAE4C",
			"text":"评论数",
			"width":     3,
			"font-size":12
		},
		{
			"type":"line",
			"values":['.$rNum.'],
			"colour":"#335ac9",
			"text":"影评数",
			"font-size":12
		}
	],
	"y_axis":{
		"stroke":1,
		"colour":"#c6d9fd",
		"max": '.$max.',
		"steps":'.$s.',
		"grid-colour":"#E4E4E4"
	},
	"x_axis":{
		"offset":false,
		"stroke":1,
		"colour":"#c6d9fd",
		"grid-colour":"#ffffff",
		"labels":{
			"labels":['.$date.'],
			"visible": true,
			"rotate":45,
			"steps":2
			}
	},
	"bg_colour":"#ffffff"
}
';
	}

	function ratingCounts(){
		require_once(APP_PATH."model/rating.php" );
		$db  = getDB();
		$data = new RatingModel($db);
		$id = $this->url['params']['2'];
		$movie = $data->getMovie($id);
		empty($movie) && exit();

		/*
		根据用户的短评、影评的打分在5.5以上评价个数，绘制出走势图；横轴是时间、纵轴是喜欢个数量，绘制成一个简单的走势图。（不显示具体数字，仅显示走势）
		*/

		$comments = $data->getTwoWeekHotComments($id);
		$reviews = $data->getTwoWeekHotReviews($id);

		$cList  = getArchive($comments);
		$rList  = getArchive($reviews);
		$dateList = getDateList();


		if(!empty($cList)){		
			$s = array_merge_recursive($cList,$rList);
			foreach($s as $k => $v ){
				if(!empty($s[$k]['count']['1'])){
					$newNum[$k]['count'] = array_sum($s[$k]['count']);
				}else{
					$newNum[$k]['count'] = $s[$k]['count'];
				}
			}
			if($newNum){
				$rMax = max($newNum);
			}
			$max = $rMax['count'];
			foreach($dateList as $k => $v ){
				if(!empty($newNum[$v])){
					$rNum .= $newNum[$v]['count'].',';
				}else{
					$rNum .= '0,';
				}
			}
			$rNum = substr($rNum,0,-1);
		}else{
			$rNum = '0,0,0,0,0,0,0,0,0,0,0,0,0,0';
		}

		foreach($dateList as $k => $v ){
			$date .= '"'.$v.'",';
		}
		$date = substr($date,0,-1);


		$steps = ceil($max / 10);
		if($max <= '0'){
			$max = '10';
		}
		
echo '
{
	"elements": [{
		"type": "area",
		"width":3,
		"dot-style": {"type":"solid-dot", "dot-size": 2, "halo": 1},
		"colour": "#838A96",
		"fill": "#E01B49",
		"fill-alpha": 0.4,
		"values": ['.$rNum.']
	}],
	"y_axis": {
		"stroke":1,
		"colour":"#c6d9fd",
		"max":'.$max.',
		"steps":'.$steps.',
		"grid-colour":"#E4E4E4"
	},
	"x_axis": {
		"offset":false,
		"stroke":1,
		"colour":"#ffffff",
		"grid-colour":"#ffffff",
		"labels": {
			"labels":['.$date.'],
			"visible": true,
			"steps":2,
			"rotate":45
		},
		"steps": 2
	},
	"bg_colour":"#ffffff"
}
';

exit();

	}

}

?>
